package com.net.xpay.admin.controller.admin;

import com.google.common.base.Preconditions;
import com.net.common.model.ResultData;
import com.net.common.model.ResultPagedList;
import com.net.common.util.MoneyUtil;
import com.net.xpay.admin.vo.PartnerWithdrawRecordStatVo;
import com.net.xpay.admin.vo.PartnerWithdrawRecordVo;
import com.net.xpay.common.domain.partner.PartnerAccountLog;
import com.net.xpay.common.domain.partner.PartnerWithdrawRecord;
import com.net.xpay.common.enums.partner.WithdrawStatus;
import com.net.xpay.common.manager.partner.PartnerAccountLogManager;
import com.net.xpay.common.manager.partner.PartnerWithdrawRecordManager;
import com.net.xpay.common.model.search.PartnerWithdrawRecordSearchModel;
import com.net.xpay.common.service.PartnerWithdrawService;
import com.net.xpay.core.enums.AccountBillType;
import com.net.xpay.core.utils.BeanCopierUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**

 * 合伙人提现记录
 */
@RestController
@RequestMapping("/admin/partner-withdraw-record")
@Api(tags = "运营/合伙人提现记录", description = "运营 合伙人提现记录 net")
public class AdminPartnerWithdrawRecordController {

    @Autowired
    private PartnerWithdrawRecordManager partnerWithdrawRecordManager;

    @Autowired
    private PartnerWithdrawService partnerWithdrawService;

    @Autowired
    private PartnerAccountLogManager partnerAccountLogManager;


    @ApiOperation("搜索合伙人提现记录")
    @PostMapping("/search")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'partner-withdraw-record::search')")
    public ResultPagedList<PartnerWithdrawRecordVo> searchPartnerWithdrawRecord(@RequestBody PartnerWithdrawRecordSearchModel searchModel) {
        List<PartnerWithdrawRecord> records = partnerWithdrawRecordManager.search(searchModel);
        List<PartnerWithdrawRecordVo> vos = BeanCopierUtil.batchCopy(PartnerWithdrawRecord.class, PartnerWithdrawRecordVo.class, records);

        for (PartnerWithdrawRecordVo vo : vos) {
            PartnerAccountLog partnerAccountLog = partnerAccountLogManager.findByRelatedIdAndType(vo.getPartnerId(), vo.getId(), AccountBillType.PARTNER_WITHDRAW);
            if (partnerAccountLog == null) {
                continue;
            }
            vo.setBeforeAmount(partnerAccountLog.getBeforeAmount());
            vo.setAfterAmount(partnerAccountLog.getAfterAmount());
        }
        long count = partnerWithdrawRecordManager.searchCount(searchModel);
        return new ResultPagedList<>(vos, count, searchModel);
    }

    @ApiOperation("取消转账")
    @PostMapping("/cancel")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ResultData<Boolean> cancelTransMoney(Long id) {
        Preconditions.checkArgument(id != null);

        partnerWithdrawService.cancelWithdrawRecord(id, "");

        return new ResultData<>(true);
    }

    @ApiOperation("按日期范围合计收益")
    @PostMapping("/sum")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'partner-withdraw-record::sum')")
    public ResultData<PartnerWithdrawRecordStatVo> sumPartnerWithdrawRecordStat(PartnerWithdrawRecordSearchModel searchModel) {
        searchModel.setStatus(null);
        long  total  = partnerWithdrawRecordManager.sumPartnerWithdrawRecordStat(searchModel);
        searchModel.setStatus(WithdrawStatus.PAYED);
        long payed = partnerWithdrawRecordManager.sumPartnerWithdrawRecordStat(searchModel);

        searchModel.setStatus(WithdrawStatus.FAILED);
        long canceled = partnerWithdrawRecordManager.sumPartnerWithdrawRecordStat(searchModel);

        total = total - canceled;

        long waitPay = total - payed;
        PartnerWithdrawRecordStatVo vo = new PartnerWithdrawRecordStatVo();
        String totalAmount = MoneyUtil.convertCentToString(total, 2);
        String payedAmount = MoneyUtil.convertCentToString(payed, 2);
        String waitPayAmount = MoneyUtil.convertCentToString(waitPay, 2);
        vo.setTotalAmount(totalAmount);
        vo.setPayedAmount(payedAmount);
        vo.setWaitPayAmount(waitPayAmount);
        return new ResultData<>(vo);
    }
}
